package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.UserIntegralDomain;
import com.ngc.mall.mapper.UserIntegralMapper;
import com.ngc.mall.service.IUserIntegralService;
import org.springframework.stereotype.Service;

/**
 *
 * 用户总积分实现类
 * @author: chenxiaolin
 * @date 2023/9/13 16:17
**/
@Service
public class UserIntegralServiceImpl extends BaseServiceImpl<UserIntegralMapper, UserIntegralDomain> implements IUserIntegralService {
    @Override
    public Boolean updateUserIntegral(Long uid, int integral) {
        UserIntegralDomain domain=super.getOne(Wrappers.<UserIntegralDomain>lambdaQuery().eq(UserIntegralDomain::getUserId,uid));
        if(domain==null){
           return super.save(UserIntegralDomain.builder().userId(uid).integralTotal(integral).build());
        }
        return super.update(Wrappers.<UserIntegralDomain>lambdaUpdate().eq(UserIntegralDomain::getUserId,uid).set(UserIntegralDomain::getIntegralTotal,domain.getIntegralTotal()+integral));
    }

    @Override
    public UserIntegralDomain getByUid(Long uid) {
        return super.getOne(Wrappers.<UserIntegralDomain>lambdaQuery().eq(UserIntegralDomain::getUserId,uid));
    }

    @Override
    public Long create(Long uid) {
        return super.create(UserIntegralDomain.builder().userId(uid).integralTotal(0).build());
    }
}
